package com.sitanyun.merchant.guide.frament.view.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.sitanyun.merchant.guide.R;
import com.sitanyun.merchant.guide.adapter.LvBankDialogAdapter;
import com.sitanyun.merchant.guide.base.BaseActivity;
import com.sitanyun.merchant.guide.base.ToastUtil;
import com.sitanyun.merchant.guide.bean.BankBean;
import com.sitanyun.merchant.guide.callback.StringCallbacks;
import com.sitanyun.merchant.guide.url.Urls;
import com.sitanyun.merchant.guide.weiht.ConvertUtil;
import com.sitanyun.merchant.guide.weiht.SharedPreferenceUtil;
import com.sitanyun.merchant.guide.weiht.StringReplaceUtil;
import com.zhy.http.okhttp.OkHttpUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * 提现
 */
public class AmountwithdrawalActivity extends BaseActivity {


    @BindView(R.id.money)
    TextView money;
    @BindView(R.id.tv_amountmoneyall)
    TextView tvAmountmoneyall;
    @BindView(R.id.tv_amountmoney)
    TextView tvAmountmoney;
    @BindView(R.id.bank)
    TextView bank;
    @BindView(R.id.sbm_withdrawal)
    TextView sbmWithdrawal;
    @BindView(R.id.ll_bank)
    LinearLayout llBank;
    @BindView(R.id.sixmoney)
    TextView sixmoney;
    private String individ;
    private BankBean bankBean;
    private LvBankDialogAdapter lvBankDialogAdapter;
    private int selectPosition = -1;//用于记录用户选择的变量
    private String cardId = "";
    private String substring;
    private int anInt;
    private String banks;
    private double aDouble;


    @Override
    public int getLayoutId() {
        return R.layout.activity_amountwithdrawal;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        setFindViewById(true);
        setTitleStr("提现");
    }

    @Override
    public void initView() {
              //赋值
        tvAmountmoneyall.setText(getIntent().getStringExtra("allmoney"));
        tvAmountmoney.setText(getIntent().getStringExtra("moneyvr"));

    }

    /**
     * 显示调用接口
     */
    @Override
    protected void onResume() {
        super.onResume();
        //  获取银行卡列表
        individ = SharedPreferenceUtil.getStringData("individ");
        OkHttpUtils.get().url(Urls.card)
                .addHeader("Authorization", "Bearer " + SharedPreferenceUtil.getStringData("token"))
                .addHeader("Content-Type", "application/json")
                .addParams("individualId", individ)
                .build().execute(new StringCallbacks() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {
                bankBean = new Gson().fromJson(response, BankBean.class);
                if (bankBean.getCode() == 0) {
                    if (bankBean.getData().size() != 0) {
                        if (bankBean.getData().get(0).getBankName().isEmpty()){
                            bank.setText("银行卡"+"("+ StringReplaceUtil.bankCardReplaceWithStar(bankBean.getData().get(0).getBankCardNum())+ ")");
                        }else {
                            bank.setText(bankBean.getData().get(0).getBankName()+"("+ StringReplaceUtil.bankCardReplaceWithStar(bankBean.getData().get(0).getBankCardNum())+ ")");

                        }
                    } else {
                        bank.setText("请添加银行卡");
                    }


                }
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    /**
     * 点击事件
     * @param view
     */
    @OnClick({R.id.bank, R.id.sbm_withdrawal, R.id.ll_bank})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            //提交
            case R.id.sbm_withdrawal:
                if (bank.getText().toString().trim().equals("请添加银行卡")) {
                    ToastUtil.showToast(AmountwithdrawalActivity.this,"请添加银行卡");
                    return;
                    // .putExtra("isIndividual", getIntent().getStringExtra("isIndividual")));
                }
                substring = tvAmountmoney.getText().toString().substring(2);
              //  substring="5500";
              //  String moneys = "6000";
                double   aDouble = ConvertUtil.convertToDouble(substring, 0);
             //   anInt = substring.compareTo(moneys);
                if (aDouble > 6000) {
                    sixmoney.setVisibility(View.VISIBLE);
                    return;
                } else {
                    sixmoney.setVisibility(View.GONE);
                    if (cardId.isEmpty()) {
                        cardId = bankBean.getData().get(0).getCardId() + "";
                    }
                    startActivity(new Intent(AmountwithdrawalActivity.this, VerifycodeActivity.class)
                            .putExtra("cardid", cardId + ""));
                    finish();
                }

                break;
            //选择卡
            case R.id.ll_bank:
            case R.id.bank:
                if (bank.getText().toString().trim().equals("请添加银行卡")) {
                    startActivity(new Intent(AmountwithdrawalActivity.this, BankInsertActivity.class));
                           // .putExtra("isIndividual", getIntent().getStringExtra("isIndividual")));
                } else {
                    initpopuview();
                }


                break;
        }
    }

    //弹窗
    private void initpopuview() {
        //1、使用Dialog、设置style
        final Dialog dialog = new Dialog(AmountwithdrawalActivity.this, R.style.DialogTheme);
        //2、设置布局
        View view = View.inflate(AmountwithdrawalActivity.this, R.layout.bankamoint_item, null);
        dialog.setContentView(view);
        ListView lvbank = view.findViewById(R.id.lv_bank);
        lvBankDialogAdapter = new LvBankDialogAdapter(AmountwithdrawalActivity.this, bankBean.getData(),bank.getText().toString().trim());
        lvbank.setAdapter(lvBankDialogAdapter);
        lvbank.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                lvBankDialogAdapter.setPosition(position);
                lvBankDialogAdapter.notifyDataSetChanged();
                cardId = bankBean.getData().get(position).getCardId();
         if (bankBean.getData().get(position).getBankName().isEmpty()){
             banks = "银行卡"+"("+StringReplaceUtil.bankCardReplaceWithStar(bankBean.getData().get(position).getBankCardNum())+")";
         }else {
             banks = bankBean.getData().get(position).getBankName()+"("+StringReplaceUtil.bankCardReplaceWithStar(bankBean.getData().get(position).getBankCardNum())+")";
         }

            }
        });
        Window window = dialog.getWindow();
        //设置弹出位置
        window.setGravity(Gravity.BOTTOM);
        //设置弹出动画
        //   window.setWindowAnimations(R.style.main_menu_animStyle);
        //设置对话框大小
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();
          //取消
        dialog.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                dialog.dismiss();
            }
        });
        //确定
        dialog.findViewById(R.id.sure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                   bank.setText(banks);
                dialog.dismiss();
            }
        });
        //添加新的银行卡
        dialog.findViewById(R.id.banksbm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               startActivity(new Intent(AmountwithdrawalActivity.this, BankInsertActivity.class));
                dialog.dismiss();
            }
        });
    }

}
